<template>
  <dragSize
    v-for="(item, index) in dialogList"
    :key="item.RID"
    :kvm="item"
    :drag-min-height="560"
    :drag-min-width="880"
    :p-index="item.RID + '_' + index"
    :is-show="true"
    :z-index="item.zIndex || 10000"
    @selectItem="topDialog"
    @openNew="openFunc"
    @close="beforeClose"
  >
    <div
      class="dialog_content"
      :class="{ 'active-terminal': activeItem?.RID === item.RID }"
      @click="topDialog(item)"
    >
      <div style="height: 100%">
        <div class="terminal-container">
          <!-- 悬浮控制按钮 -->
          <div class="floating-controls">
            <!-- 只读状态切换按钮 -->
            <div
              v-if="showReadonlyIndicator"
              class="control-indicator status-readonly-indicator"
              :class="{ active: radioMap[item.RID].read }"
              :title="radioMap[item.RID].read ? '只读模式' : '可编辑模式'"
              @click="toggleReadOnly(item)"
            >
              <svg
                v-if="!radioMap[item.RID].read"
                width="20"
                height="20"
                viewBox="0 0 24 24"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M20.71 7.04c.39-.39.39-1.04 0-1.41l-2.34-2.34c-.37-.39-1.02-.39-1.41 0l-1.84 1.83 3.75 3.75M3 17.25V21h3.75L17.81 9.93l-3.75-3.75L3 17.25z"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
              </svg>
              <svg
                v-else
                width="20"
                height="20"
                viewBox="0 0 24 24"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"
                  fill="currentColor"
                />
              </svg>
            </div>

            <!-- 软键盘按钮 -->
            <div
              class="control-indicator keyboard-indicator"
              title="打开VNC配置 (Ctrl+Shift+Alt)"
              @click="openSystemKeyboard(item)"
            >
              <svg
                width="20"
                height="20"
                viewBox="0 0 24 24"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15Z"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
                <path
                  d="M19.4 15C19.2669 15.3016 19.2272 15.6362 19.286 15.9606C19.3448 16.285 19.4995 16.5843 19.73 16.82L19.79 16.88C19.976 17.0657 20.1235 17.2863 20.2241 17.5291C20.3248 17.7719 20.3766 18.0322 20.3766 18.295C20.3766 18.5578 20.3248 18.8181 20.2241 19.0609C20.1235 19.3037 19.976 19.5243 19.79 19.71C19.6043 19.896 19.3837 20.0435 19.1409 20.1441C18.8981 20.2448 18.6378 20.2966 18.375 20.2966C18.1122 20.2966 17.8519 20.2448 17.6091 20.1441C17.3663 20.0435 17.1457 19.896 16.96 19.71L16.9 19.65C16.6643 19.4195 16.365 19.2648 16.0406 19.206C15.7162 19.1472 15.3816 19.1869 15.08 19.32C14.7842 19.4468 14.532 19.6572 14.3543 19.9255C14.1766 20.1938 14.0813 20.5082 14.08 20.83V21C14.08 21.5304 13.8693 22.0391 13.4942 22.4142C13.1191 22.7893 12.6104 23 12.08 23C11.5496 23 11.0409 22.7893 10.6658 22.4142C10.2907 22.0391 10.08 21.5304 10.08 21V20.91C10.0723 20.579 9.96512 20.258 9.77251 19.9887C9.5799 19.7194 9.31074 19.5143 9 19.4C8.69838 19.2669 8.36381 19.2272 8.03941 19.286C7.71502 19.3448 7.41568 19.4995 7.18 19.73L7.12 19.79C6.93425 19.976 6.71368 20.1235 6.47088 20.2241C6.22808 20.3248 5.96783 20.3766 5.705 20.3766C5.44217 20.3766 5.18192 20.3248 4.93912 20.2241C4.69632 20.1235 4.47575 19.976 4.29 19.79C4.10405 19.6043 3.95653 19.3837 3.85588 19.1409C3.75523 18.8981 3.70343 18.6378 3.70343 18.375C3.70343 18.1122 3.75523 17.8519 3.85588 17.6091C3.95653 17.3663 4.10405 17.1457 4.29 16.96L4.35 16.9C4.58054 16.6643 4.73519 16.365 4.794 16.0406C4.85282 15.7162 4.81312 15.3816 4.68 15.08C4.55324 14.7842 4.34276 14.532 4.07447 14.3543C3.80618 14.1766 3.49179 14.0813 3.17 14.08H3C2.46957 14.08 1.96086 13.8693 1.58579 13.4942C1.21071 13.1191 1 12.6104 1 12.08C1 11.5496 1.21071 11.0409 1.58579 10.6658C1.96086 10.2907 2.46957 10.08 3 10.08H3.09C3.42099 10.0723 3.742 9.96512 4.0113 9.77251C4.28059 9.5799 4.48572 9.31074 4.6 9C4.73312 8.69838 4.77282 8.36381 4.714 8.03941C4.65519 7.71502 4.50054 7.41568 4.27 7.18L4.21 7.12C4.02405 6.93425 3.87653 6.71368 3.77588 6.47088C3.67523 6.22808 3.62343 5.96783 3.62343 5.705C3.62343 5.44217 3.67523 5.18192 3.77588 4.93912C3.87653 4.69632 4.02405 4.47575 4.21 4.29C4.39575 4.10405 4.61632 3.95653 4.85912 3.85588C5.10192 3.75523 5.36217 3.70343 5.625 3.70343C5.88783 3.70343 6.14808 3.75523 6.39088 3.85588C6.63368 3.95653 6.85425 4.10405 7.04 4.29L7.1 4.35C7.33568 4.58054 7.63502 4.73519 7.95941 4.794C8.28381 4.85282 8.61838 4.81312 8.92 4.68H9C9.29577 4.55324 9.54802 4.34276 9.72569 4.07447C9.90337 3.80618 9.99872 3.49179 10 3.17V3C10 2.46957 10.2107 1.96086 10.5858 1.58579C10.9609 1.21071 11.4696 1 12 1C12.5304 1 13.0391 1.21071 13.4142 1.58579C13.7893 1.96086 14 2.46957 14 3V3.09C14.0013 3.41179 14.0966 3.72618 14.2743 3.99447C14.452 4.26276 14.7042 4.47324 15 4.6C15.3016 4.73312 15.6362 4.77282 15.9606 4.714C16.285 4.65519 16.5843 4.50054 16.82 4.27L16.88 4.21C17.0657 4.02405 17.2863 3.87653 17.5291 3.77588C17.7719 3.67523 18.0322 3.62343 18.295 3.62343C18.5578 3.62343 18.8181 3.67523 19.0609 3.77588C19.3037 3.87653 19.5243 4.02405 19.71 4.21C19.896 4.39575 20.0435 4.61632 20.1441 4.85912C20.2448 5.10192 20.2966 5.36217 20.2966 5.625C20.2966 5.88783 20.2448 6.14808 20.1441 6.39088C20.0435 6.63368 19.896 6.85425 19.71 7.04L19.65 7.1C19.4195 7.33568 19.2648 7.63502 19.206 7.95941C19.1472 8.28381 19.1869 8.61838 19.32 8.92V9C19.4468 9.29577 19.6572 9.54802 19.9255 9.72569C20.1938 9.90337 20.5082 9.99872 20.83 10H21C21.5304 10 22.0391 10.2107 22.4142 10.5858C22.7893 10.9609 23 11.4696 23 12C23 12.5304 22.7893 13.0391 22.4142 13.4142C22.0391 13.7893 21.5304 14 21 14H20.91C20.5882 14.0013 20.2738 14.0966 20.0055 14.2743C19.7372 14.452 19.5268 14.7042 19.4 15Z"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
              </svg>
            </div>
          </div>

          <div class="dockerIframe" v-loading="loading">
            <div
              :class="[
                'iframeMain',
                radioMap[item.RID].read ? 'divReadonly' : '',
              ]"
            >
              <Terminal
                :ref="
                  el => {
                    if (el) terminalRefs[item.RID] = el;
                  }
                "
                :url="single ? '' : url"
                :singleId="dockerID"
                :data-rid="item.RID"
              ></Terminal>
            </div>
            <div
              :class="['mask', activeItem?.RID === item.RID ? 'active' : '']"
              @click="topDialog(item)"
            />
          </div>

          <!-- 命令输入控制台 -->
          <div
            class="command-console"
            v-if="!radioMap[item.RID].read"
            @click="handleCommandInputClick(item)"
          >
            <div class="console-prefix">$</div>
            <input
              type="text"
              class="console-input"
              v-model="commandInputs[item.RID]"
              @keydown.enter="sendCommand(item)"
              @keydown.stop
              placeholder="输入命令，回车发送（不支持中文）"
              @focus="currentCommandItem = item"
              :id="'CommandInput' + item.RID"
              autocomplete="off"
              inputmode="text"
              @compositionstart.prevent
              @compositionend.prevent
              @input="handleInput"
              @keydown="handleInputKeyDown"
            />
            <div
              class="console-send-btn"
              @click.stop="sendCommand(item)"
              title="发送命令"
            >
              <svg
                viewBox="0 0 24 24"
                width="20"
                height="20"
                stroke="currentColor"
                stroke-width="2"
                fill="none"
                stroke-linecap="round"
                stroke-linejoin="round"
              >
                <line x1="22" y1="2" x2="11" y2="13"></line>
                <polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
              </svg>
            </div>
          </div>
        </div>
      </div>
    </div>
  </dragSize>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref, defineExpose, nextTick } from 'vue';
import dragSize from './dragSize.vue';
import Terminal from './dockerTerminal.vue';
import { ElMessage } from 'element-plus';

// 定义item类型接口
interface DialogItem {
  RID: number;
  id?: string | number;
  zIndex?: number;
  linkType?: boolean;
  uri?: string;
  token?: string;
}

const emit = defineEmits(['closeDialog', 'topDialog']);
const props = defineProps({
  dialogList: {
    default: function () {
      return [] as DialogItem[];
    },
    type: Array as () => DialogItem[],
  },
  single: Boolean,
  showReadonlyIndicator: {
    type: Boolean,
    default: true,
  },
});

//保存read、loading等信息
const radioMap = ref({});
const url = ref('');
const dockerID = ref('');
const hostOrigin = ref('');
const activeItem = ref(null);
const loading = ref(false);

// 命令输入相关
const commandInputs = ref({});
const commandHistories = ref({});
const historyIndices = ref({});
const currentCommandItem = ref(null);

// 源地址信息
const mode = import.meta.env.MODE;
hostOrigin.value = window.location.host;

const terminalRefs = ref({});

/**
 * 初始化终端的命令历史记录
 */
const initTerminalHistory = rid => {
  if (!commandHistories.value[rid]) {
    commandHistories.value[rid] = [];
  }
  if (historyIndices.value[rid] === undefined) {
    historyIndices.value[rid] = -1;
  }
  if (commandInputs.value[rid] === undefined) {
    commandInputs.value[rid] = '';
  }
};

/**
 * 聚焦到命令输入框
 */
const focusCommandInput = () => {
  nextTick(() => {
    try {
      const activeRID = activeItem.value?.RID;
      if (!activeRID) return;

      const inputElement = document.getElementById('CommandInput' + activeRID);
      if (inputElement && inputElement instanceof HTMLElement) {
        inputElement.focus();
      }
    } catch (e) {
      console.error('聚焦输入框失败:', e);
    }
  });
};

/**
 * 发送命令到终端
 */
const sendCommand = item => {
  const input = commandInputs.value[item.RID];
  if (!input?.trim()) return;

  // 检查是否包含中文字符
  if (/[\u4e00-\u9fa5]/.test(input)) {
    ElMessage.warning('不支持输入中文，请使用英文');
    return;
  }

  // 初始化该终端的历史记录
  initTerminalHistory(item.RID);

  // 记录到历史
  commandHistories.value[item.RID].push(input);
  if (commandHistories.value[item.RID].length > 50) {
    commandHistories.value[item.RID].shift(); // 保留最近50条命令
  }
  historyIndices.value[item.RID] = -1;

  // 发送命令到终端
  const command = input + '\n';
  const terminal = terminalRefs.value[item.RID];
  console.log('测试', terminal);

  if (terminal?.socket) {
    // 通过 WebSocket 发送命令
    terminal.socket.send(command);
  } else {
    // 如果找不到 WebSocket 连接，尝试通过事件发送
    const terminalElement = document.querySelector(
      `.terminal[data-rid="${item.RID}"]`,
    );
    if (terminalElement) {
      terminalElement.dispatchEvent(
        new KeyboardEvent('keydown', {
          key: command,
          code: 'Enter',
          keyCode: 13,
          which: 13,
          bubbles: true,
        }),
      );
    }
  }

  // 清空当前终端的输入框
  commandInputs.value[item.RID] = '';

  // 重新聚焦输入框
  focusCommandInput();
};

// 添加输入框事件监听
const handleInputKeyDown = (event: KeyboardEvent) => {
  // 阻止输入法编辑器的输入
  if (event.key === 'Process' || event.key === 'Composition') {
    event.preventDefault();
    return;
  }

  // 允许控制键和特殊键
  if (event.ctrlKey || event.altKey || event.metaKey) {
    return;
  }

  // 允许方向键、删除键等
  if (
    event.key === 'Backspace' ||
    event.key === 'Delete' ||
    event.key === 'ArrowLeft' ||
    event.key === 'ArrowRight' ||
    event.key === 'ArrowUp' ||
    event.key === 'ArrowDown' ||
    event.key === 'Tab' ||
    event.key === 'Enter'
  ) {
    return;
  }

  // 只允许英文字母、数字和特殊字符
  const allowedChars = /^[a-zA-Z0-9\s\-_.,;:!@#$%^&*()+=<>?/\\|`~]$/;
  if (!allowedChars.test(event.key)) {
    event.preventDefault();
  }
};

const handleInput = (event: InputEvent) => {
  const input = event.target as HTMLInputElement;
  // 直接移除中文字符，不显示提示
  input.value = input.value.replace(/[\u4e00-\u9fa5]/g, '');
};

/**
 * 切换只读模式
 */
const toggleReadOnly = item => {
  if (item) {
    radioMap.value[item.RID].read = !radioMap.value[item.RID].read;
    changeDocker(item, radioMap.value[item.RID].read);

    ElMessage({
      message: radioMap.value[item.RID].read
        ? '已切换为只读模式'
        : '已切换为可编辑模式',
      type: 'success',
      duration: 1500,
    });
  }
};

/**
 * 打开系统软键盘
 */
const openSystemKeyboard = item => {
  const terminal = document.querySelector('.terminal');
  if (terminal) {
    // 模拟Ctrl+Shift+Alt组合键
    const ctrlDown = new KeyboardEvent('keydown', {
      key: 'Control',
      code: 'ControlLeft',
      keyCode: 17,
      which: 17,
      ctrlKey: true,
      bubbles: true,
    });
    terminal.dispatchEvent(ctrlDown);

    const shiftDown = new KeyboardEvent('keydown', {
      key: 'Shift',
      code: 'ShiftLeft',
      keyCode: 16,
      which: 16,
      shiftKey: true,
      ctrlKey: true,
      bubbles: true,
    });
    terminal.dispatchEvent(shiftDown);

    const altDown = new KeyboardEvent('keydown', {
      key: 'Alt',
      code: 'AltLeft',
      keyCode: 18,
      which: 18,
      altKey: true,
      ctrlKey: true,
      shiftKey: true,
      bubbles: true,
    });
    terminal.dispatchEvent(altDown);

    // 释放按键
    setTimeout(() => {
      terminal.dispatchEvent(
        new KeyboardEvent('keyup', {
          key: 'Alt',
          code: 'AltLeft',
          keyCode: 18,
          which: 18,
          altKey: false,
          ctrlKey: true,
          shiftKey: true,
          bubbles: true,
        }),
      );
      terminal.dispatchEvent(
        new KeyboardEvent('keyup', {
          key: 'Shift',
          code: 'ShiftLeft',
          keyCode: 16,
          which: 16,
          shiftKey: false,
          ctrlKey: true,
          bubbles: true,
        }),
      );
      terminal.dispatchEvent(
        new KeyboardEvent('keyup', {
          key: 'Control',
          code: 'ControlLeft',
          keyCode: 17,
          which: 17,
          ctrlKey: false,
          bubbles: true,
        }),
      );
    }, 100);
  }
};

/**
 * 修改docker控制台状态
 */
const changeDocker = (item, read) => {
  openFunc(item, read);
};

/**
 * 打开控制台
 */
const openFunc = async (item, read) => {
  radioMap.value[item.RID] = { read, loading: true };
  dockerID.value = item.id;
  const mode = import.meta.env.MODE;
  url.value =
    mode === 'development'
      ? `ws://172.17.0.29:12001/simulation/scene/scene/${item['id']}/instance/${item['RID']}/console/`
      : `wss://${hostOrigin.value}/api/simulation/scene/scene/${item['id']}/instance/${item['RID']}/console/`;
  activeItem.value = item;

  // 初始化该终端的历史记录
  initTerminalHistory(item.RID);

  // 在弹窗打开后自动聚焦命令输入框
  nextTick(() => {
    if (!radioMap.value[item.RID].read) {
      focusCommandInput();
    }
  });
};

/**
 * 点击弹窗设置层级
 */
const topDialog = item => {
  activeItem.value = item;
  emit('topDialog', item);
};

/**
 * 关闭弹窗
 */
const beforeClose = item => {
  closeFunc(item);
  emit('closeDialog', item);
};

function closeFunc(item) {
  delete radioMap.value[item.RID];
}

/**
 * socket信息
 */
const messageFunc = async (event: MessageEvent) => {
  const message = event.data;
  if (!message) {
    setTimeout(() => {
      closeFunc(activeItem.value);
      emit('closeDialog', activeItem.value);
    }, 1000);
  }
  if (message === 'Docker Websocket Connection Failed!') {
    setTimeout(() => {
      closeFunc(activeItem.value);
      emit('closeDialog', activeItem.value);
    }, 1000);
  }
};

onMounted(async () => {
  window.addEventListener('message', messageFunc);
});

onUnmounted(async () => {
  window.removeEventListener('message', messageFunc);
});

defineExpose({
  openFunc,
});

/**
 * 处理命令输入框点击事件
 */
const handleCommandInputClick = (item: DialogItem) => {
  // 设置当前激活的终端
  activeItem.value = item;

  // 聚焦到命令输入框
  focusCommandInput();
};
</script>

<style lang="scss" scoped>
.divReadonly {
  pointer-events: none;
  cursor: default;
}

.dialog_content {
  width: 100%;
  height: 100%;
  background-color: #0c0c0c;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  border: 1px solid #222;
  display: flex;
  flex-direction: column;
  position: relative;

  &.active-terminal {
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(50, 205, 50, 0.2);
    border-color: #2d2d2d;
  }
}

.terminal-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background-color: #121212;
  position: relative;
}

.dockerIframe {
  padding: unset;
  border-radius: unset;
  width: 100%;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
  transition: height 0.3s ease;

  &.full-height {
    height: 100%;
  }

  .iframeMain {
    width: 100%;
    height: 100%;
    transition: opacity 0.3s ease;
    position: relative;

    &.readonly {
      pointer-events: none;
      -moz-user-select: none;
      -webkit-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
  }

  .mask {
    width: calc(100% - 2px);
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    transition: background-color 0.2s ease, height 0.2s ease;

    &:not(.active) {
      background-color: rgba(0, 0, 0, 0.05);
      backdrop-filter: blur(0.5px);
    }

    &.active {
      height: 0;
    }
  }
}

/* 命令控制台样式 */
.command-console {
  width: 100%;
  height: 60px;
  background: linear-gradient(to bottom, #252525, #1a1a1a);
  border-top: 1px solid #333;
  display: flex;
  align-items: center;
  padding: 0 16px;
  box-sizing: border-box;
  cursor: text;
  transition: background 0.3s ease;
  margin-top: auto;

  &:hover {
    background: linear-gradient(to bottom, #272727, #1c1c1c);
  }

  .console-prefix {
    color: #32cd32;
    font-family: 'Consolas', monospace;
    font-weight: bold;
    font-size: 18px;
    margin-right: 12px;
    animation: blink 1.5s step-end infinite;
  }

  .console-input {
    flex: 1;
    background-color: rgba(0, 0, 0, 0.25);
    border: 1px solid #444;
    outline: none;
    color: #f0f0f0;
    font-family: 'Consolas', monospace;
    font-size: 16px;
    line-height: 24px;
    height: 40px;
    padding: 0 12px;
    border-radius: 4px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
    transition: all 0.2s ease;
    caret-color: #32cd32;
    margin-right: 10px;

    &:focus {
      background-color: rgba(0, 0, 0, 0.3);
      border-color: #666;
      box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.4),
        0 0 0 2px rgba(50, 205, 50, 0.2);
    }

    &::placeholder {
      color: #888;
    }
  }

  .console-send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 4px;
    background-color: rgba(50, 205, 50, 0.1);
    color: #32cd32;
    cursor: pointer;
    transition: all 0.2s ease;

    &:hover {
      background-color: rgba(50, 205, 50, 0.2);
      transform: translateY(-2px);
      box-shadow: 0 2px 8px rgba(50, 205, 50, 0.15);
    }

    &:active {
      background-color: rgba(50, 205, 50, 0.3);
      transform: scale(0.95) translateY(0);
      box-shadow: 0 1px 3px rgba(50, 205, 50, 0.2);
    }

    svg {
      transition: transform 0.2s ease;
    }

    &:hover svg {
      transform: translateX(2px);
    }
  }
}

/* 悬浮控制按钮 */
.floating-controls {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 8px;
  z-index: 100;
  opacity: 0.8;
  transition: all 0.3s ease;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 6px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.3);

  &:hover {
    opacity: 1;
    transform: translateY(-1px);
  }

  .control-indicator {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background-color: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 16px;

    &:hover {
      background-color: rgba(255, 255, 255, 0.25);
      border-color: rgba(255, 255, 255, 0.3);
      transform: translateY(-1px);
    }

    &.active {
      background-color: rgba(255, 189, 46, 0.2);
      border-color: rgba(255, 189, 46, 0.5);
      color: #ffbd2e;

      &:hover {
        background-color: rgba(255, 189, 46, 0.3);
      }
    }

    &:not(.active) {
      background-color: rgba(50, 205, 50, 0.2);
      border-color: rgba(50, 205, 50, 0.4);
      color: #32cd32;

      &:hover {
        background-color: rgba(50, 205, 50, 0.3);
      }
    }

    &:hover {
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
      background-color: rgba(0, 0, 0, 0.7);
      color: #fff;
    }

    &:active {
      transform: translateY(0);
    }

    &.keyboard-indicator {
      background-color: rgba(50, 205, 50, 0.2);
      border-color: rgba(50, 205, 50, 0.4);
      color: #32cd32;

      &:hover {
        background-color: rgba(50, 205, 50, 0.3);
      }

      i {
        animation: pulse-green 4s infinite;
      }

      svg {
        animation: pulse-green 4s infinite;
      }
    }

    &.status-readonly-indicator {
      background-color: rgba(50, 205, 50, 0.2);
      border-color: rgba(50, 205, 50, 0.4);
      color: #32cd32;

      svg {
        animation: pulse-green 4s infinite;
      }
    }

    &.status-readonly-indicator.active {
      background-color: rgba(255, 189, 46, 0.2);
      border-color: rgba(255, 189, 46, 0.5);
      color: #ffbd2e;

      &:hover {
        background-color: rgba(255, 189, 46, 0.3);
      }

      i {
        animation: pulse-yellow 2s infinite;
      }

      svg {
        animation: pulse-yellow 2s infinite;
      }
    }

    svg {
      width: 18px; /* 增大SVG图标 */
      height: 18px;
    }
  }
}

@keyframes blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@keyframes icon-pulse {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0.1;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.5);
    opacity: 0.05;
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0.1;
  }
}
</style>
